//
//  ExhibitionView.m
//  BCVersion
//
//  Created by 吴 崧 on 14-3-8.
//  Copyright (c) 2014年 Jungle. All rights reserved.
//

#import "ExhibitionView.h"
static int retryTime = 0;
@implementation ExhibitionView

- (instancetype)initWithURL:(NSString *)urlString
{
    self = [[NSBundle mainBundle]loadNibNamed:@"ExhibitionView" owner:nil options:nil][0];
    if (self) {
        // Initialization code
        NSURL *requsetURL = [[NSURL alloc]initWithString:urlString];
        _request = [[NSURLRequest alloc]initWithURL:requsetURL];
    }
    return self;
}

- (instancetype)initWithString:(NSString *)string
{
    self = [[NSBundle mainBundle]loadNibNamed:@"ExhibitionView" owner:nil options:nil][0];
    if (self) {
        // Initialization code
        self.htmlString = string;
    }
    return self;
}

-(void)didMoveToSuperview
{
    [super didMoveToSuperview];
    [_exhibitionWebView setDelegate:self];
    if(self.htmlString != nil)
    {
        [_exhibitionWebView loadHTMLString:self.htmlString baseURL:nil];
    }else{
    [_exhibitionWebView loadRequest:_request];
    }
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(stopEdit:)];
	[self addGestureRecognizer:tapGr];
}




- (void)stopEdit:(UITapGestureRecognizer *)gestureRecognizer {
	[self dimissFromSuperView:nil];
}


-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    if(retryTime < 3)
    {
        if(self.htmlString != nil)
        {
        [webView loadHTMLString:self.htmlString baseURL:nil];
        }
        else
        {
        [webView loadRequest:webView.request];
        }
        retryTime += 1;
    }
}

- (IBAction)dimissFromSuperView:(UIButton *)sender {
    [_exhibitionWebView stopLoading];
    _exhibitionWebView = nil;
    [self removeFromSuperview];
}


-(void)Show
{
    self.alpha = 0.0f;
    [APP_Delegate.window addSubview:self];
    [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        
        self.alpha = 1.0f;
    } completion:^(BOOL finished) {
        
    }];

}



@end
